﻿using GatewayClient.Models;
using GatewayClient.Device;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading;
using UtilsBase;

namespace GatewayClient.Service
{
    /// <summary>
    /// 处理指令
    /// </summary>
    public class ServiceSystemTime
    {


        /// <summary>
        /// 系统时间更新
        /// </summary>
        /// <param name="obj"></param>
        public void handle(string dateTime)//  public void handle(MQReqModel<object> mQReqModel)
        {
            //if (DateTime.Parse(dateTime) <= ModelSystemTime.systemTime)
            //{//如果时间比本地小，不更新
            //    return;
            //}


            ModelSystemTime.systemTime = DateTime.Parse(dateTime);
            Console.WriteLine("\n set systemTime:" + ModelSystemTime.systemTime.ToString("yyyy-MM-dd HH:mm:ss"));

            // if (System.DateTime.Now > ModelSystemTime.systemTime.AddMinutes(1) || System.DateTime.Now < ModelSystemTime.systemTime.AddMinutes(-1))
            // {
            try
            {
                ///更新系统时间
                //UtilsProcess utilsProcess = new UtilsProcess();
                //utilsProcess.executeBashCommand("sudo ntpdate 0.cn.pool.ntp.org");//zero2更新时间


                UtilsProcess utilsProcess = new UtilsProcess();
                utilsProcess.executeBashCommand("date -s '" + ModelSystemTime.systemTime.ToString("yyyy-MM-dd HH:mm:ss") + "'");//更新时间

                //更新RTC 模块时间
                //Thread.Sleep(1000);
                // utilsProcess.executeBashCommand("sudo hwclock -w -f /dev/rtc1");//zero2更新时间
                // Thread.Sleep(1000);
                // utilsProcess.executeBashCommand("sudo hwclock -r -f /dev/rtc1");//zero2更新时间
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            // }

        }
    }
}
